package com.idp.web.shop.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.model.Response;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;
import com.idp.web.shop.entity.GoodsProp;
import com.idp.web.shop.service.GoodsPropService;

/**
 * 商品规格controller
 * 	2018-04-12 WuJiangWu
 */
@Controller
@RequestMapping("/goodsProp")
public class GoodsPropController extends BaseController {

	private Logger logger = Logger.getLogger(GoodsPropController.class);

	@Resource
	private GoodsPropService goodsPropService;
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	初始化查询页面
	 * @return
	 */
	@RequestMapping("/init")
	public String init(){
		
		return "shop/goodsProp/goodsPropSearch";
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * @param goodsProp
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/list")
	public String list(GoodsProp goodsProp,Page<GoodsProp> page,HttpServletRequest request){
		try {
			request.setAttribute("page", goodsPropService.findByPage(goodsProp, page));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shop/goodsProp/goodsPropList";
	}

	/**
	 * 	2018-04-12 WuJiangWu
	 * 	新增修改页面初始化
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/goodsProp")
	public String goodsProp(String id,HttpServletRequest request){
		try {
			if(ValidateUtils.isNotEmpty(id)){
				
				GoodsProp goodsProp = goodsPropService.getById(id);
				request.setAttribute("goodsProp", goodsProp);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shop/goodsProp/goodsProp";
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	保存
	 * @param goodsProp
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Response save(GoodsProp goodsProp){
		Response json = new Response();
		try {
			// 修改
			if(ValidateUtils.isNotEmpty(goodsProp.getId())){
				goodsPropService.update(goodsProp);
			}
			// 新增
			else{
				goodsPropService.add(goodsProp);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setErrcode(-1);
			json.setMsg("保存失败");
		}
		
		return json;
	}
	
	/**
	 * 	2018-04-12 WuJiangWu
	 * 	删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Response delete(String id){
		Response json = new Response();
		try {
			goodsPropService.delete(id);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.setErrcode(-1);
			json.setMsg("删除失败");
		}
		return json;
	}
}
